﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UBlockly.Compotent
{
    [DisallowMultipleComponent]
    [RequireComponent(typeof(RectTransform))]
    public abstract class BlockAnchor : MonoBehaviour
    {
        internal List<IUAnchor> subAnchorList;
        public RectTransform RectTransform => GetComponent<RectTransform>();

        protected virtual void Awake()
        {
            subAnchorList = new List<IUAnchor>(3);
            RectTransform.anchorMin = new Vector2(0, 0.5f);
            RectTransform.anchorMax = new Vector2(0, 0.5f);
            RectTransform.pivot = new Vector2(0, 0.5f);
        }

        public virtual void AttachToBlock(BlockAnchor parentBlock)
        {
            SetParent(parentBlock.transform);
        }
        public virtual void SetParent(Transform parent)
        {
            transform.SetParent(parent);
            RectTransform.anchorMin = new Vector2(0, 0.5f);
            RectTransform.anchorMax = new Vector2(0, 0.5f);
            RectTransform.pivot = new Vector2(0, 0.5f);
        }
        public Vector2 GetSize()
        {
            return RectTransform.sizeDelta;
        }
      
        /// <summary>
        /// 设置尺寸
        /// </summary>
        /// <param name="size"></param>
        public void SetSize(Vector2 size)
        {
            RectTransform.sizeDelta = size;
        }

        protected virtual void UpdateSubAuchorList()
        {
            if (0 == transform.childCount)
                return;
            subAnchorList.Clear();
            for (int i = 0; i < transform.childCount; i++)
            {
                var cc = transform.GetChild(i).GetComponent<IUAnchor>();
                if (cc != null)
                    subAnchorList.Add(cc);
            }
        }
    }
}
